/**
 * 
 */
package test.gm

import junit.framework.TestCase
import org.juiceshop.gm.*import java.lang.IndexOutOfBoundsException

public class PhraseTest extends TestCase{
	
	NoteFactory nf = new NoteFactory()
	
    Note C = nf.note(60)
    Note F2 = nf.note(65)/2
    Note G4 = nf.note(67)/4
    Note R = nf.note(Note.REST)
	
    Chord ch = new Chord(F2,G4)
    
    Phrase p
    Phrase p1
    Phrase p2
    
	void testBasic() {
		p = new Phrase()
		assertEquals 0, p.size()
		assertEquals 0d, p.value
        p = new Phrase(C)
        assertEquals 1, p.size()
        assertEquals 1d, p.value
        p = new Phrase(C,F2)
        assertEquals 2, p.size()
        assertEquals 1.5d, p.value
        p = new Phrase(C,F2,G4)
        assertEquals 3, p.size()
        assertEquals 1.75d, p.value
        p = new Phrase(ch)
        assertEquals 1, p.size()
        assertEquals 0.5d, p.value
		p = new Phrase(ch,C)
        assertEquals 2, p.size()
        assertEquals 1.5d, p.value
        p = new Phrase(C,ch)
        assertEquals 2, p.size()
        assertEquals 1.5d, p.value
        p = new Phrase(ch,ch)
        assertEquals 2, p.size()
        assertEquals 1d, p.value
	}
    
    void testGetAt() {
    	p = new Phrase(C,F2,G4)
    	assertEquals F2.pitch, p[1].pitch
    	try {
    		p[3]
    		fail "Must throw IndexOutOfBoundsException"
    	} catch (IndexOutOfBoundsException x) { 
    		// OK
    	}
    }
    
    void testMultiplyDiv() {
        p = new Phrase(C,F2,G4)
        assertEquals 3, p.size()
        assertEquals 1.75d, p.value
        p1 = p*2
        assertEquals 3, p1.size()
        assertEquals 1.75d*2, p1.value
        p2 = p/2
        assertEquals 3, p2.size()
        assertEquals 1.75d/2, p2.value
    }
    
    
    void testOr() {
        p = new Phrase(C,F2,G4)
    	p1 = p | R
        assertEquals 4, p1.size()
        assertEquals 2.75d, p1.value
        p2 = p | p
        assertEquals 6, p2.size()
        assertEquals 1.75d*2, p2.value        
    }
    
	
}
